/*
* This file is part of RoboDS (http://code.google.com/p/robods/)
* Copyright (C) 2007 Blakharaz
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

#include "MoveRobotsState.h"

#include "Card.h"
#include "JobQueue.h"
#include "Logger.h"
#include "AbstractJob.h"
#include "Robot.h"
#include "RobotController.h"
#include "Game.h"

namespace robo
{

MoveRobotsState::MoveRobotsState(Game* game)
:   GameState(game),
    mCurrentRobot(BEFORE_FIRST_ROBOT),
    mCurrentCard(0)
{
}

MoveRobotsState::~MoveRobotsState()
{
}

GameState::StateType MoveRobotsState::getType() const
{
	return GameState::ROBOT_MOVEMENT;
}

void MoveRobotsState::resume()
{
	mCurrentRobot = BEFORE_FIRST_ROBOT;
}

bool MoveRobotsState::OrderByCardPrio::operator()(Card* lhs, Card* rhs)
{
	return lhs->getPriority() < rhs->getPriority();
}

void MoveRobotsState::calculateJobs(Robot* robot, Card* card, JobQueue* queue)
{
	static unsigned short FRAMES_PER_BOT = 30;

	switch (card->getType())
	{
	case Card::CARD_MOVEMENT_3:
		queue->add(mGame->createMovementJob(
			robot, robot->getMapPosition(), robot->getMapPositionWhenMoving(1), FRAMES_PER_BOT/3));
		queue->add(mGame->createMovementJob(
			robot, robot->getMapPositionWhenMoving(1), robot->getMapPositionWhenMoving(2), FRAMES_PER_BOT/3));
		queue->add(mGame->createMovementJob(
			robot, robot->getMapPositionWhenMoving(2), robot->getMapPositionWhenMoving(3), FRAMES_PER_BOT/3));
		break;
	case Card::CARD_MOVEMENT_2:
		queue->add(mGame->createMovementJob(
			robot, robot->getMapPosition(), robot->getMapPositionWhenMoving(1), FRAMES_PER_BOT/2));
		queue->add(mGame->createMovementJob(
			robot, robot->getMapPositionWhenMoving(1), robot->getMapPositionWhenMoving(2), FRAMES_PER_BOT/2));
		break;
	case Card::CARD_MOVEMENT_1:
		queue->add(mGame->createMovementJob(
			robot, robot->getMapPosition(), robot->getMapPositionWhenMoving(1), FRAMES_PER_BOT));
		break;
	case Card::CARD_MOVEMENT_BACK:
		queue->add(mGame->createMovementJob(
			robot, robot->getMapPosition(), robot->getMapPositionWhenMoving(-1), FRAMES_PER_BOT));
		break;
	case Card::CARD_TURN_LEFT:
	case Card::CARD_TURN_RIGHT:
	case Card::CARD_TURN_AROUND:
		queue->add(mGame->createTurnJob(
			robot, card->getType(), FRAMES_PER_BOT));
		break;
	}
}

}
